﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Gencode.Gengui.Forms
{
    public partial class GUDialogBox : GUDialogForm
    {
        UserControl control;

		bool useEscBtn = true;
		/// <summary>
		/// Close Dialog when Escape is pressed
		/// </summary>
		public bool UseEscapeButton
		{
			set
			{
				useEscBtn = value;
			}
		}

		/// <summary>
		/// Gets the returnvalue of the IReturnValueDialog. Returns null if dialog has no return value.
		/// </summary>
		public object ReturnValue
		{
			get
			{
				if (control is IReturnValueDialog)
					return ((IReturnValueDialog)control).ReturnValue;

				return null;
			}
		}

        /// <summary>
        /// Construktor
        /// </summary>
        /// <param name="control">UserControl must implement Personalplaner.Dialog.IDialogControl</param>
        /// <param name="caption">Caption of the Dialog</param>
        public GUDialogBox(UserControl control, string caption)
        {
            InitializeComponent();
            this.Text = caption;
            if (control is IDialogControl)
            {
                this.control = control;
                ControlPanel.Panel1.Controls.Add(this.control);

                SetSize();
            }
        }

        private void SetSize()
        {
			this.Width = control.Size.Width + 8;
            this.Height = control.Size.Height + (this.Height - ControlPanel.Panel1.Height) + 8;
            ButtonPosition();
			this.control.Dock = DockStyle.Fill;
        }

        private void ButtonPosition()
        {
            Point c = new Point(this.Width - btnCancle.Width - 20, btnCancle.Location.Y);
            Point o = new Point(this.Width - btnCancle.Width - btnOK.Width - 30, btnOK.Location.Y);

            btnCancle.Location = c;
            btnOK.Location = o;

			if (control is ISaveDialog)
			{
				Point s = new Point(this.Width - btnCancle.Width - btnOK.Width -btnSpeichern.Width - 40, btnCancle.Location.Y);
				btnSpeichern.Location = s;
				btnSpeichern.Visible = true;
			}
			else
				btnSpeichern.Visible = false;

			if (control is ISaveNewDialog)
			{
				Point n = new Point(this.Width - btnCancle.Width - btnOK.Width - btnSpeichern.Width - btnNew.Width - 50, btnCancle.Location.Y);
				btnNew.Location = n;
				btnNew.Visible = true;
			}
			else
				btnNew.Visible = false;

			if (control is INewOKCancelDialog)
			{
				Point n = new Point(this.Width - btnCancle.Width - btnOK.Width - btnNew.Width - 40, btnCancle.Location.Y);
				btnNew.Location = n;
				btnNew.Visible = true;
			}
			else
			{
				ISaveNewDialog tmp = control as ISaveNewDialog;
				if (tmp == null)
					btnNew.Visible = false;
			}

			if (control is IDeleteNewSaveDialog)
			{
				Point n = new Point(this.Width - btnCancle.Width - btnOK.Width - btnSpeichern.Width - btnNew.Width - btnDelete.Width - 60, btnCancle.Location.Y);
				btnDelete.Location = n;
				btnDelete.Visible = true;
			}
			else
				btnDelete.Visible = false;
        }

        private void OKCancleDialog_ResizeEnd(object sender, EventArgs e)
        {
            ButtonPosition();
        }

        private void OKCancleDialog_Load(object sender, EventArgs e)
        {
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            IDialogControl cont = control as IDialogControl;
			if (!cont.OKClicked())
				DialogResult = DialogResult.None;
				//this.Close();
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            IDialogControl cont = control as IDialogControl;
			if (!cont.CancleClicked())
				DialogResult = DialogResult.None;
				//this.Close();
        }

		private void btnSpeichern_Click(object sender, EventArgs e)
		{
			ISaveDialog cont = control as ISaveDialog;
			cont.SaveClicked();
		}

		private void btnNew_Click(object sender, EventArgs e)
		{
			if (control is INewOKCancelDialog){
				INewOKCancelDialog cont = control as INewOKCancelDialog;
				cont.NewClicked();
			}

			if (control is ISaveNewDialog)
			{
				ISaveNewDialog cont = control as ISaveNewDialog;
				cont.NewClicked();
			}
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			IDeleteNewSaveDialog cont = control as IDeleteNewSaveDialog;
			cont.DeleteClicked();
		}
    }
}
